package virtualstock.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import virtualstock.request.GameController;
import virtualstock.request.PlayerController;
import virtualstock.util.GameDetails;
import virtualstock.util.PlayerDetails;

/**
 * Servlet implementation class AdminControlServlet
 */
public class AdminControlServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@EJB
	private GameController gameController;
	
	@EJB
	private PlayerController playerController;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminControlServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    
    private void processListGame(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
    	PrintWriter out = response.getWriter();
    	
    	List<GameDetails> gameList = null;  	
    	gameList = gameController.getAllAvailableGames();
    	
    	out.println("<h2><span>List of games in the system</span></h2>");
		out.println("<form name=\"gameRadioForm\" action=\"GameRemoveServlet\"  method='post'>");
		out.println("<table id='gameListTable' border='3'>");
		out.println("<tr >");
		out.println("    <th bgcolor=>select</th>");
		out.println("    <th bgcolor=>GameID</th>");
		out.println("    <th bgcolor=>Name</th>");
		out.println("    <th bgcolor=>CreatorID</th>");
//		out.println("    <th bgcolor=>MaxPlayer</th>");
		out.println("    <th bgcolor=>InitBalance</th>");
		out.println("    <th bgcolor=>StartDate</th>");
		out.println("    ");
		out.println("</tr>");
		if(gameList != null){
			for (Iterator<GameDetails> it = gameList.iterator(); it.hasNext(); ) {
				GameDetails details = it.next();
				out.println("<tr>");
				out.println("    <td><input type=\"radio\" name=\"gameID\" value=\"" + details.getGameID() + "\" /></td>"); 
				out.println("    <td>" + details.getGameID() + "&nbsp;&nbsp;</td>");
				out.println("    <td>" + details.getGameName() + "&nbsp;&nbsp;</td>"); 
				out.println("    <td>" + details.getCreator() + "&nbsp;&nbsp;</td>"); 
//				out.println("    <td>" + details.getMaxPlayer() + "&nbsp;&nbsp;</td>"); 
				out.println("    <td>" + details.getInitialBalance() + "&nbsp;&nbsp;</td>");
				out.println("    <td>" + details.getStartDate() + "&nbsp;&nbsp;</td>");
				out.println("</tr>"); 	
			}
		}
		out.println("</table>");
		out.println("<input type='submit' value='remove'/>");
		out.println("</form>");
		out.println("<br />");
		response.setHeader("Cache-Control", "no-cache");
    	
    }
    
    private void processListPlayer(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
    	PrintWriter out = response.getWriter();
    	
    	List<PlayerDetails> playerList = null;
    	playerList = playerController.getAllPlayer();
    	
    	out.println("<h2><span>List of players in the system</span></h2>");
		out.println("<form name=\"gameRadioForm\" action=\"PlayerRemoveServlet\"  method='post'>");
		out.println("<table id='playerListTable' border='3'>");
		out.println("<tr >");
		out.println("    <th bgcolor=>select</th>");
		out.println("    <th bgcolor=>PlayerID</th>");
		out.println("    <th bgcolor=>FirstName</th>");
		out.println("    <th bgcolor=>LastName</th>");
		out.println("    <th bgcolor=>Email</th>");
		out.println("    <th bgcolor=>Password</th>");
		out.println("    ");
		out.println("</tr>");
		if(playerList != null){
			for (Iterator<PlayerDetails> it = playerList.iterator(); it.hasNext(); ) {
				PlayerDetails details = it.next();
				out.println("<tr>");
				out.println("    <td><input type=\"radio\" name=\"playerID\" value=\"" + details.getPlayerID() + "\" /></td>"); 
				out.println("    <td>" + details.getPlayerID() + "&nbsp;&nbsp;</td>"); 
				out.println("    <td>" + details.getFirstName() + "&nbsp;&nbsp;</td>"); 
				out.println("    <td>" + details.getLastName() + "&nbsp;&nbsp;</td>"); 
				out.println("    <td>" + details.getEmail() + "&nbsp;&nbsp;</td>");
				out.println("    <td>" + details.getPassword() + "&nbsp;&nbsp;</td>");
				out.println("</tr>"); 	
			}
		}
		out.println("</table>");
		out.println("<input type='submit' value='remove'/>");
		out.println("</form>");
		out.println("<br />");
		response.setHeader("Cache-Control", "no-cache");
    }
    
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		if (request.getSession().getAttribute("adminlogin") == null) {
			out.println("admin session expired.");
			return;
		}
		
		String query = request.getQueryString();
		
		int index = query.lastIndexOf('=');
		String type = query.substring(index+1);
		
		if(type.equals("game")){
			processListGame(request, response);
			
		}
		else if(type.equals("player")){
			processListPlayer(request, response);
		}
		else{
			out.println("type error in AdminControlServlet!");
		}
	
		response.setHeader("Cache-Control", "no-cache");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	
	}

}
